﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Diagnostics;

namespace RelationTextBox
{
    public class RelationTextBoxCell : DataGridViewTextBoxCell
    {
        private EventHandler _MyButtonClick = null;
        public EventHandler MyButtonClick
        {
            get { return _MyButtonClick; }
            set
            {
                _MyButtonClick = value;
            }
        }
        public RelationTextBoxCell()
        {
   
        }
        public override Type EditType
        {
            get
            {
                return typeof(RelationTextBoxControl);
            }
        }
        /// <summary>
        /// implementarea functiei InitializeEditingControl . Functia este apelata de controlul DataGridView 
        /// la inceputul sesiunii de editare. Se asigura ca proprietatile controlului de editare 
        /// sunt in conformitate cu propritatile celulei.
        /// </summary>
        public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
        {
            base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
            RelationTextBox edControl = this.DataGridView.EditingControl as RelationTextBox;
            if (edControl != null)
            {
                edControl.BorderStyle = BorderStyle.None;
                if (!edControl.HasButtonHandler())
                {
                    //daca nu are handler
                    if (this.MyButtonClick != null)
                        edControl.ButtonClick += this.MyButtonClick;
                    else
                    {
                        int indexCol = DataGridView.CurrentCell.ColumnIndex;
                        if (DataGridView.Columns[indexCol].CellTemplate.GetType() == typeof(RelationTextBoxCell))
                            edControl.ButtonClick +=
                                ((RelationTextBoxCell)(DataGridView.Columns[indexCol].CellTemplate)).MyButtonClick;
                    }
                }
                else 
                { 
                    // daca are dar e diferit.. se pune cel nou...
                    if (this.MyButtonClick != null &&  !edControl.IsHandler(this.MyButtonClick))
                    {
                        edControl.ClearHandler();
                        edControl.ButtonClick += this.MyButtonClick;
                    }
                    
                }
                string initialFormattedValueStr = initialFormattedValue as string;
                if (initialFormattedValueStr == null)
                {
                    edControl.Text = string.Empty;
                }
                else
                {
                    edControl.Text = initialFormattedValueStr;
                }
            }
        }
        /// <summary>
        /// DetachEditingControl este apelata de controlul DataGridView cand se incheie sesiunea de editare
        /// </summary>
        [
            EditorBrowsable(EditorBrowsableState.Advanced)
        ]
        public override void DetachEditingControl()
        {
            if (DataGridView == null || DataGridView.EditingControl == null)
            {
                throw new InvalidOperationException("Cell is detached or its grid has no editing control.");
            }

            RelationTextBox relButton = DataGridView.EditingControl as RelationTextBox;
            if (relButton != null)
            {
                // Editing controls get recycled. Indeed, when a DataGridViewNumericUpDownCell cell gets edited
                // after another DataGridViewNumericUpDownCell cell, the same editing control gets reused for 
                // performance reasons (to avoid an unnecessary control destruction and creation). 
                // Here the undo buffer of the TextBox inside the NumericUpDown control gets cleared to avoid
                // interferences between the editing sessions.
                if (relButton.txBRelation != null)
                {
                    relButton.txBRelation.ClearUndo();
                }
            }

            base.DetachEditingControl();
        }
    }
}
